#!/bin/bash
#
# Written by Assaf Paz (damagedspline@gmail.com)
# License: GPL2
#
# Simple RRQ handler for tftp used for debugging tftp servers
#

NETCAT_CMD=netcat
CLIENT_PORT=1230
DELAY_TIME=2

if [ ! $# -eq 4 ]; then
        echo Usage: $0 \<filename\> \<mode\> \<server address\> \<server port\>
        echo mode must be either "octet" or "netascii"
        echo other parameters are not checked for authencity - so we\'re counting on you.
        exit 1
fi

case $2 in
        octet|netascii)
                ;;
        *)
                echo Bad mode - should be octet or netascii
                exit 1
                ;;
esac

echo creating the rrq request for file \<$1\> in mode \<$2\> from $3:$4
echo -e -n "\00""\001"$1"\00"$2"\00" | $NETCAT_CMD -u $3 $4 -p $CLIENT_PORT -q 0
echo "request sent successfully!"

echo "start sending acks!"

(for (( j=0 ; j < 255 ; j=j+1 ));do
        for (( i=1 ; i < 256 ; i=i+1 )); do
	        echo -n -e "\00""\004""\0$(printf "%o" $j)""\0$(printf "%o" $i)"
	        sleep $DELAY_TIME
        done
        echo -n -e "\00""\004""\000)""\0$(printf "%o" $j)"
	sleep $DELAY_TIME
done) | $NETCAT_CMD -u -l -p $CLIENT_PORT



